<?php

namespace app\admin\controller;

use app\model\AnnModel;
use support\Request;
use support\Response;
use Exception;

class AnnController
{
    /**
     * 获取公告列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            
            $query = AnnModel::query()
                ->whereNull('deleted_at')
                ->orderBy('id', 'desc');
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            foreach ($list as &$item) {
                $item['created_at'] = date('Y-m-d H:i:s', $item->created_at);
                $item['updated_at'] = date('Y-m-d H:i:s', $item->updated_at);
            }
                
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取公告详情
     * @param Request $request
     * @return Response
     */
    public function detail(Request $request): Response
    {
        try {
            $id = $request->get('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $ann = AnnModel::whereNull('deleted_at')->find($id);
            if (!$ann) {
                throw new Exception('公告不存在');
            }

            $ann['created_at'] = date('Y-m-d H:i:s', $ann->created_at);
            $ann['updated_at'] = date('Y-m-d H:i:s', $ann->updated_at);

            return json([
                'data' => $ann,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 提交/创建公告
     * @param Request $request
     * @return Response
     */
    public function submit(Request $request): Response
    {
        try {
            $data = $request->post();
            
            if (empty($data['ann_title']) || empty($data['ann_content'])) {
                throw new Exception('公告标题和内容不能为空');
            }

            // 判断是新建还是编辑
            if (!empty($data['id'])) {
                $ann = AnnModel::whereNull('deleted_at')->find($data['id']);
                if (!$ann) {
                    throw new Exception('公告不存在');
                }
            } else {
                $ann = new AnnModel();
            }

            $ann->ann_title = $data['ann_title'];
            $ann->ann_content = $data['ann_content'];
            $ann->updated_at = time();
            
            // 新建时设置创建时间
            if (empty($data['id'])) {
                $ann->created_at = time();
            }
            
            if (!$ann->save()) {
                throw new Exception(empty($data['id']) ? '添加失败' : '更新失败');
            }

            return json([
                'data' => $ann,
                'msg' => empty($data['id']) ? '添加成功' : '更新成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除公告（软删除）
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        try {
            $id = $request->post('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $ann = AnnModel::whereNull('deleted_at')->find($id);
            if (!$ann) {
                throw new Exception('公告不存在');
            }

            if (!$ann->softDelete()) {
                throw new Exception('删除失败');
            }

            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 